#ifndef _DSWUPGRADE_H
#define _DSWUPGRADE_H

#include "base.h"

typedef struct{
	int nFileType;
	int nFileLen;
	int nCheckSum;
} __attribute__ ((packed)) ST_UPGRADE_FILE_HEADER_T, *LPST_UPGRADE_FILE_HEADER_T;

typedef enum{
	FIRMWARE_APP = 100,
	FIRMWARE_SYS = 200,
	FIRMWARE_CFG = 300,
	FIRMWARE_FS = 400,
} EN_FIRMWARE_TYPE;

class CDswupgrade
{
public:
	CDswupgrade(LPST_ENV_T pEnv, LPST_FIRMWARE_T pFirmware, char* pMTDDev);
	virtual ~CDswupgrade();
public:
	static CDswupgrade* createNew(LPST_ENV_T pEnv, LPST_FIRMWARE_T pFirmware);
	static bool IsValidFirmware(LPST_FIRMWARE_T pFirmware, char* pmtddev);
private:
	static void* cbUpgradeThreadFunc(void* pParam){((CDswupgrade*)pParam)->UpgradeThreadFunc();return NULL;};
	void UpgradeThreadFunc();
	bool FlashEraseBlock(int ndev, unsigned long nblockaddr, unsigned long nerasesize);
	void UpdateBootCfg(int ndev, struct mtd_info_user *pMtd, char* pBuffer);
private:
	LPST_ENV_T m_pEnv;
	ST_FIRMWARE_T m_nFirmware;
	char m_sMTD[64];
	pthread_t m_thread_id;
	bool m_bEndThread;
};

#endif

